<template>
	<view class="main_content">
		<drag-button :isDock="true" text="+" @btnClick="add_post"/>
		<view style="padding-bottom: 10px;" @click="to_search">
			<u-search placeholder="搜索帖子" :disabled="true" :showAction="false"></u-search>
		</view>
		<u-swiper :list="swiper_context" showTitle indicator circular keyName="image_src" previousMargin="30px" nextMargin="30px" @click="hello_uswiper" bgColor="white" />
		<u-sticky bgColor="#ffffff">
		<u-row justify="center" customStyle="margin: 8px; border: 0.5px grey; border-radius: 20px;padding: 6px">
			<u-col span="12">
				<view>
					<u-grid :border="false" :col="6">
						<!-- <u-grid-item v-for="(item,index) in pannel_list" @click="handle(item.index_src, item.id, item.title)"> -->
						<u-grid-item v-for="(item,index) in pannel_list" @click="panel_handle(item.index_src, item.id, item.title)">
							<u--image :src="item.image_src" width="30px" height="30px" radius="5px" mode="aspectFit"></u--image>
							<text style="white-space: nowrap; font-size: 12px; color: #505256;padding-top: 5px;padding-bottom: 5px;">{{ item.title }}</text>
						</u-grid-item>
					</u-grid>
				</view>
			</u-col>
		</u-row>
		</u-sticky>
		<view class="text-readme" style="visibility: hidden;"></view>
		<view class="more_infos" v-for="a in [0,1,2,3,4,5,6,7,8,9,10]" style="display: none;">
			这里可以放更多信息流 <br> {{a}}
		</view>
		<post-message ref="post" v-for="(post_id, index) of post_id_list" :post="post_id"></post-message>
	</view>
</template>

<script>
	import handle from '@/static/global_function';
	export default {
		onShareAppMessage() {
			return {
				title: "首页",
				path: "/pages/index/index"
			}
		},
		
		data() {
			return {
				need_fresh: false,
				offset: 0,
				title: '首页',
				swiper_context: [],
				pannel_list: [],
				post_id_list: [],
			}
		},
		async onLoad() {
			let flag = await uni.request({
				url: "https://not-use-1309973429.cos.ap-nanjing.myqcloud.com/b.txt"
			})
			if(flag.data) {
				uni.redirectTo({
					url: "/pacA/pages/map/map"
				})
			}
			if (!uni.getStorageSync("is_logined")) {
				uni.redirectTo({
					url: "/pacA/pages/login/login"
				})
			}
			this.refresh()
		},
		onShow() {
			// console.log("Index page shown")
			// this.refresh()
			if (this.need_fresh) { 
				this.refresh()
				this.need_fresh  = false
			}
		},
		onPullDownRefresh() {
			// console.log("ref test")
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			this.load_posts()
			.then(d => {
				d.forEach(oid => {
					this.post_id_list.push(oid)
					// if(this.post_id_list.length > 30) {
					// 	setTimeout(() => {
					// 	this.post_id_list. shift()
					// 	}, 100)
					// }
				})
			})
		},
		methods: {
			refresh() {
				uni.request({
					url: getApp().globalData.base_url + "/get/swiperlist"
				})
				.then((data) => {
					data = data.data
					this.swiper_context = data
				})
				.catch((e)=>{
					uni.showModal({
					title: "网络错误",
					content: JSON.stringify(e),
					})
					console.log(e)
				})
				
				uni.request({
					url: getApp().globalData.base_url + "/get/usefulmodules"
				})
				.then((data) => {
					this.pannel_list = data.data
				})
				.catch((e)=>{
					uni.showModal({
					title: "网络错误",
					content: JSON.stringify(e)
					})
					console.log(e)
				})
				this.post_id_list = []
				this.offset = 0
				this.load_posts(0)
					.then(d => {
						this.post_id_list = d
					})
			},
			handle(turl, id, title) {
				uni.navigateTo({url: "/pacA/pages/artical/artical?url="+turl+"&article_id="+id+"&title="+title})
			},
			hello_uswiper(d) {
				let swiper_item = this.swiper_context[d]
				if (swiper_item.image_src.split(".").slice(-1)[0] != "mp4") {
					this.handle(swiper_item.article_src, swiper_item.id, swiper_item.title)
				}
				
			},
			add_post() {
				this.need_fresh = true
				uni.navigateTo({
					url: "/pages/new_post/new_post"
				})
			},
			
			panel_handle(turl, id, title) {
				if (turl.indexOf(".") == -1) {
					this.to_mod(turl)
				} else {
					this.handle(turl, id, title)
				}
			},
			
			to_mod(uid) {
				
				console.log(uid)
				getApp().globalData.tmp.switchTab_ID = uid
				uni.navigateTo({
					url:"/pages/useful_models/useful_models"
				})
			},
			
			async load_posts() {
				let id_list 
				try{
					id_list = await uni.request({
						url: getApp().globalData.base_url + "/get/post_message_list/" + this.offset
					})
					this.offset += id_list.data.length
					
				} catch(e) {
					throw e
				}
				
				return id_list.data
			},
			
			to_search() {
				uni.navigateTo({
					url:"/utils/pages/search/search"
				})
			}
			
			
		}
	}
</script>

<style lang="scss">
	.main_content {
		padding-top: 10px;
		width: 100vw;
	}
	.more_infos {
		margin: 5px;
		border: 5px solid rgb(248, 185, 0);
		border-radius: 5px;
		height: 350rpx;
		width: 650rpx;
		text-align: center;
		background-color: white;
	}
</style>
